/**
 * Copyright (C) 2009 Noel Ang
 *
 * This file is part of Samsara.
 *
 * Samsara is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Samsara is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Samsara.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package com.lunarshades.samsara.ui;

import java.util.ArrayList;
import java.util.List;
import javax.swing.RowFilter;
import javax.swing.SortOrder;
import javax.swing.table.TableRowSorter;

/**
 * A TableRowSorter that understands {@link Filterable} elements.
 *
 * @author Noel Ang, sparta.variable@gmail.com
 * @see Filterable
 */
public class RosterTableRowSorter<T extends RosterTableModel>
        extends TableRowSorter<T> {

public RosterTableRowSorter(T model, int initialSortedColumnIndex) {
    super(model);
    setSortsOnUpdates(true);

    // Use the first column for initial sorting
    List<SortKey> sortkeys = new ArrayList<SortKey>(model.getColumnCount());
    sortkeys.add(new SortKey(initialSortedColumnIndex, SortOrder.ASCENDING));
    setSortKeys(sortkeys);

    // Filter for hiding from view Filterable objects whose Filterable.isINcluded == false
    setRowFilter(new RowFilter<T, Integer>() {
        public boolean include(Entry<? extends T, ? extends Integer> entry) {
            T model = entry.getModel();
            Object value = model.getRowAt(entry.getIdentifier());
            return Filterable.class.isInstance(value)
                    && ((Filterable) value).isIncluded();
        }
    }
    );
}
}
